home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / zerodisk.zip / ZERODISK.C < prev    next >
C/C++ Source or Header  |  1989-03-06  |  1KB  |  49 lines

  1. /* zerodisk: erase unallocated space from disk */
  2. /* Steve Creps (creps@silver.bacs.indiana.edu), March 6, 1989 */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <io.h>
  7. #include <malloc.h>
  8. #include <string.h>
  9.  
  10. #define BLOCKSIZE ((unsigned int)1024)
  11.  
  12. static char tmpfn[] = "zerodisk.tmp";
  13.  
  14. int
  15. main(argc, argv)
  16. int argc;
  17. char *argv[];
  18. {
  19.     char *buf, *tmppath;
  20.     FILE *fp;
  21.     int output;
  22.  
  23.     if (argc > 2) {
  24.         fprintf(stderr, "Usage: zerodisk [device]");
  25.         exit(-1);
  26.     }
  27.     if ((buf = (char *)calloc(BLOCKSIZE, sizeof(char))) == 0) {
  28.         fprintf(stderr, "zerodisk: cannot calloc %u bytes.\n" , BLOCKSIZE);
  29.         exit(-1);
  30.     }
  31.     if (argc == 2) {
  32.         unsigned int l = strlen(argv[1]) + strlen(tmpfn) + 1;
  33.         if ((tmppath = (char *)malloc(l)) == 0) {
  34.             fprintf(stderr, "zerodisk: cannot malloc %u bytes.\n", l);
  35.             exit(-1);
  36.         }
  37.         sprintf(tmppath, "%s%s%c", argv[1], tmpfn, (char)0);
  38.     } else sprintf(tmppath, "%s%c", tmpfn, (char)0);
  39.     if ((fp = fopen(tmppath, "wb")) == 0) {
  40.         fprintf(stderr, "zerodisk: cannot open %s.", tmppath);
  41.         exit(-1);
  42.     }
  43.     output = fileno(fp);
  44.     while (write(output, buf, BLOCKSIZE) >= 0) ;
  45.     (void)fclose(fp);
  46.     unlink(tmppath);
  47.     return 0;
  48. }
  49.